//app.js
const util = require('./utils/util.js')
App({
    globalData: {
        token: "",
        userInfo: null,
        code: '',
        systemInfo: {}, // 系统信息
        reqHost: 'https://apitest.dudubashi.com', // 请求接口域名
        // reqHost: 'https://wx.dudubashi.com', // 请求接口域名
        srcHost: 'https://oss.dudubashi.com', // 资源路径，放到cdn中 形如 https://oss.dudubashi.com/min_app_src/driver/banner.png
        link: 'https://wx.dudubashi.com',
        page_link: 'https://wx.dudubashi.com/index.php/feo/shuttle/home' // 设置h5链接的页面
    },
    onLaunch: function (options) {
        var self = this
        var query = {}
        self.getCode() // 获取code
        // 根据传递的域名参数进行设置请求的域名环境
        if (options && options.query) {
            query = options.query || {}
            if (query.req) {
                self.globalData.reqHost = decodeURIComponent(query.req)
            }
        }
        if (!self.globalData.systemInfo || !self.globalData.systemInfo['SDKVersion']) {
            self.getSystemInfo() // 获取系统信息
        }
    },
    getCode: function () {
        var self = this
        // 登录
        wx.login({
            success: res => {
                // 发送 res.code 到后台换取 openId, sessionKey, unionId
                self.globalData.code = res.code
            }
        })
    },
    getSystemInfo: function () {
        var self = this
        // 获取系统信息
        wx.getSystemInfo({
            success: function (res) {
                self.globalData.systemInfo = res || {}
            },
            fail: function (res) {
            },
            complete: function (res) {
            },
        })
    },
    uploadLog: function (type, param) {
        // 上传日志数据，小程序数据统计
        var self = this
        var _param = param || {}
        if (self.globalData.systemInfo) {
            // 添加系统信息，使用备注字段
            _param.system_info = ''
            if (self.globalData.systemInfo['SDKVersion']) {
                _param.system_info = 'sdk版本' + self.globalData.systemInfo['SDKVersion']
            }
            if (self.globalData.systemInfo['version']) {
                _param.system_info = _param.system_info + '+微信版本:' + self.globalData.systemInfo['version']
            }
        }
        if (!_param.log_time) {
            // 添加上传日志时间
            _param.log_time = util.formatDate('', 'yyyy-MM-dd hh:mm:ss')
        }
        if (type && wx.reportAnalytics) {
            wx.reportAnalytics(type, _param)
        }
    },
    // 统一请求方法
    request(options) {
        const {
            url,
            method = "POST",
            data = {},
            header = {},
            noToken = false,
        } = options;
        return new Promise((resolve, reject) => {
            // 构建请求头
            const requestHeader = {
                "content-type": "application/x-www-form-urlencoded",
                ...header,
            };

            // 添加token
            if (!noToken && this.globalData.token) {
                requestHeader.Authorization = this.globalData.token;
            }

            wx.request({
                url: this.globalData.reqHost + url,
                method,
                data,
                header: requestHeader,
                success: (res) => {
                    // 请求成功
                    if (res.statusCode === 200) {
                        if (res.data.result === "0000") {
                            resolve(res.data);
                        } else {
                            reject(res.data);
                        }
                    } else {
                        reject({
                            code: res.statusCode,
                            message: "请求失败",
                        });
                    }
                },
                fail: (err) => {
                    reject({
                        code: -1,
                        message: "网络错误",
                        error: err,
                    });
                },
            });
        });
    },

})